app.controller('MarketCtrl', ['$scope', '$http', 'GoodsService', function($scope, $http, GoodsService){

	//获取market数据
	$http.get('data/data.json').then(function(res){
		console.log(res);
		//获取全部数据
		$scope.allData = res.data.data;
		//分类数据
		$scope.categories = $scope.allData.categories;
		//默认商品数据
		$scope.goods = $scope.allData.products[$scope.categories[0].id];
		// console.log($scope.goods);
		//所有商品数据初始化为0;
		$scope.goods.map(function(item){
			item.count = 0;
		});
	});

	// 点击切换商品数据
	$scope.currentIndex = 0;
	$scope.changeTab = function(){
		$scope.currentIndex = this.$index;
		// console.log($scope.currentIndex);
		var id = this.item.id;
		$scope.goods = $scope.allData.products[id];
		$scope.goods.map(function(item){
			item.count = 0;
		});
	};

	// 增加商品
	$scope.add = function(){
		this.item.count++;
		// 传入service
		GoodsService.addGood(this.item);
		console.log(GoodsService.allGoods());
	}

	// 减少商品
	$scope.reduce = function(){
		if(this.item.count != 1){
			this.item.count--;
		}else{
			this.item.count = 0;
		}
		// 传入service
		GoodsService.reduceCount(this.item);
		console.log(GoodsService.allGoods());

	}
	

}])